<template>
  <div class="info">
  <el-form  :model='searchInfo' inline>
    <el-form-item>
      <el-input clearable v-model="searchInfo.name" placeholder="请输入电影名" />
    </el-form-item>
    <el-form-item>
      <el-select v-model="searchInfo.movieStatus" placeholder="请选择">
        <el-option label="正在上映" value="1"></el-option>
        <el-option label="已上映" value="2"></el-option>
        <el-option label="未上映" value="3"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-date-picker v-model="searchInfo.releaseDateArr" value-format="yyyy-MM-dd" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
      <el-button icon="el-icon-refresh-right" @click="resetForm">重置</el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="openWin()">添加</el-button>
    </el-form-item>
  </el-form>
  <el-table :data=movieList style="width: 100%" border >
    <el-table-column label="编号" prop="movieId"  align="center" />
    <el-table-column label="电影名称" prop="name"  align="center" show-overflow-tooltip />
    <el-table-column label="翻译名" prop="translationName"  align="center" show-overflow-tooltip />
    <el-table-column label="放映时间" prop="releaseDate"  align="center" />
    <el-table-column label="电影状态" prop="movieStatus" align="center">
      <template slot-scope="scope">
      <el-button type="primary" v-if="scope.row.movieStatus===1">正在上映</el-button>
      <el-button type="success" v-if="scope.row.movieStatus===2">已上映</el-button>
      <el-button type="danger" v-if="scope.row.movieStatus===3">未上映</el-button>
      </template>
    </el-table-column>
    <el-table-column label="状态" align="center">
      <template slot-scope="scope">
        <el-button type="success" v-if="scope.row.status===1" @click="changStatus(scope.row)">启用</el-button>
        <el-button v-else type="danger" @click="changStatus(scope.row)" >禁用</el-button>
      </template>
    </el-table-column>

    <el-table-column label="查看" align="center">
      <template slot-scope="scope">
        <el-button type="text" @click="getMovie(scope.row.movieId)">电影详情</el-button>
      </template>
      </el-table-column>
    <el-table-column label="操作"  align="center">
      <template slot-scope="scope">
        <el-button type="success" size="small" @click="openWin(scope.row)">修改</el-button>
        <el-button type="danger" size="small" @click="delInfo(scope.row.movieId)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
    <el-dialog title="电影详情" :visible.sync="dialogVisible" width="70%">
      <el-form  ref="form" :model="film" label-position="right" label-width="100px">
        <el-form-item label="电影名称" prop="name">
          <el-input v-model="film.name" disabled></el-input>
        </el-form-item >
        <el-form-item label="翻译名" prop="translationName">
          <el-input v-model="film.translationName" disabled>
          </el-input>
        </el-form-item>
        <el-form-item label="电影介绍" prop="introduction"  >
          <el-input v-model="film.introduction" disabled ></el-input>
        </el-form-item>
        <el-form-item label="发布日期" prop="releaseDate" >
          <el-input
            v-model="film.releaseDate" disabled >
          </el-input>
        </el-form-item>
        <el-form-item label="导演" prop="director">
          <el-input v-model="film.director" disabled></el-input>
        </el-form-item>
        <el-form-item label="演员" prop="actors">
          <el-input v-model="film.actors" disabled></el-input>
        </el-form-item>
        <el-form-item label="电影类型" prop="actors">
          <el-input v-model="film.types" disabled></el-input>
        </el-form-item>
        <el-form-item label="电影剪影" prop="images">
          <el-image style="width: 100px;height: 100px;margin-right: 10px" v-for="item in this.fileList" :key="item.name" :src='item.url' :preview-src-list="fileInfo"></el-image>
        </el-form-item>
        <el-form-item>
            <el-button @click="getComment(film.movieId)">查看影评</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-pagination layout="total,prev,pager,next,sizes,jumper"
                   :total="pageInfo.totalCount"
                   :page-size="pageInfo.pageSize"
                   :current-page="pageInfo.currPage"
                   :page-sizes="[5,10,20,30,50,100]"
                   @size-change="sizeChange"
                   @current-change="currentChange"
    />
  </div>
</template>

<script>

export default {
  name: 'FileInfo',
  data () {
    return {
      fileList: [],
      fileInfo: [],
      movieList: [],
      actorInfo: [],
      commentList: [],
      searchInfo: {
        movieStatus: '',
        releaseDateArr: [],
        name: '',
        page: 1,
        limit: 5
      },
      pageInfo: {},
      dialogVisible: false,
      film: {
        movieId: '',
        name: '',
        translationName: '',
        releaseDate: '',
        movieStatus: '',
        director: '',
        actorList: [],
        typeList: [],
        imageList: []
      }
    }
  },
  methods: {
    getList () {
      const si = {
        ...this.searchInfo,
        releaseDateArr: this.searchInfo.releaseDateArr.toString()
      }
      this.$http.get('/sys/movie/list', si).then(({ data }) => {
        if (data.code === 200) {
          this.movieList = data.data.list
          this.pageInfo = data.data
        } else {
          this.$confirm(data.msg)
        }
      })
    },
    resetForm () {
      this.searchInfo.name = ''
      this.searchInfo.movieStatus = ''
      this.searchInfo.releaseDateArr = []
      this.getList()
    },
    openWin (id) {
      if (id) {
        this.$router.push('filmEdit?movieId=' + id.movieId)
      } else {
        this.$router.push('filmEdit')
      }
    },
    delInfo (id) {
      this.$confirm('确定删除该电影吗', '询问').then(() => {
        this.$axios.delete(`/sys/movie/delete/${id}`).then(({ data }) => {
          if (data.code === 200) {
            this.$message({
              message: '删除成功',
              type: 'success',
              onClose: () => {
                this.getList()
              }
            })
          } else {
            this.$alert('删除失败!', { type: 'error' })
          }
        })
      })
    },
    sizeChange (limit) {
      this.searchInfo.limit = limit
      this.getList()
    },
    currentChange (page) {
      this.searchInfo.page = page
      this.getList()
    },
    changStatus (movie) {
      this.$confirm(`确定${movie.status === 1 ? '禁用' : '启用'}该电影吗？`).then(() => {
        this.$http.post('/sys/movie/status', { movieId: movie.movieId, status: movie.status === 1 ? 0 : 1 }).then(({ data }) => {
          if (data.code === 200) {
            this.$message.success('更改状态成功')
            this.getList()
          } else {
            this.$confirm(data.msg)
          }
        })
      })
    },
    getMovie (movieId) {
      this.dialogVisible = true
      this.$http.get('/sys/movie/info/' + movieId).then(({ data }) => {
        const movieList = data.data.movie
        const actor = data.data.actorList
        const actorId = []
        const type = data.data.typeList
        const typeId = []
        const image = data.data.imageList
        for (let i = 0; i < actor.length; i++) {
          if (actor[i].status === 1) {
            actorId.push(actor[i].actorName)
          }
        }
        for (let i = 0; i < type.length; i++) {
          typeId.push(type[i].typeName)
        }
        for (let i = 0; i < this.actorInfo.length; i++) {
          if (movieList.director === this.actorInfo[i].actorId) {
            movieList.director = this.actorInfo[i].actorName
          }
        }
        if (data.code === 200) {
          this.fileList = image.map(item => {
            return {
              name: item.imageSrc,
              url: `${process.env.VUE_APP_BASE_API}/img/${item.imageSrc}`
            }
          })
          for (let i = 0; i < this.fileList.length; i++) {
            this.fileInfo.push(this.fileList[i].url)
          }
          this.film = {
            movieId: movieList.movieId,
            name: movieList.name,
            translationName: movieList.translationName,
            introduction: movieList.introduction,
            releaseDate: movieList.releaseDate,
            director: movieList.director,
            movieStatus: movieList.movieStatus,
            actors: actorId.join(','),
            types: typeId.join(','),
            imageList: data.data.imageList
          }
        }
      })
    },
    getActor () {
      this.$http.get('/sys/actor/list').then(({ data }) => {
        if (data.code === 200) {
          this.actorInfo = data.page.list
        } else {
          this.$confirm(data.msg)
        }
      })
    },
    getComment (Id) {
      this.$router.push('filmReview?movieId=' + Id)
    }
  },
  created () {
    this.$nextTick(() => {
      this.getList()
      this.getActor()
    })
  }
}
</script>

<style scoped>

</style>
